// Copyright 2013 Google Inc. All Rights Reserved.

#include <errno.h>
#include <time.h>

// clock_nanosleep is not available in NaCl IRT, so we define
// clock_nanosleep() by ourselves.
int clock_nanosleep(clockid_t clock_id, int flags,
                    const struct timespec *request,
                    struct timespec *remain) {
    if (request->tv_sec < 0 ||
        request->tv_nsec < 0 || request->tv_nsec > 999999999) {
        return -EINVAL;
    }

    if (flags != TIMER_ABSTIME) {
        return nanosleep(request, remain);
    }

    struct timespec now;
    int ret = clock_gettime(clock_id, &now);
    if (ret == -1)
        return -1;

    struct timespec to_sleep = *request;
    to_sleep.tv_sec -= now.tv_sec;
    to_sleep.tv_nsec -= now.tv_nsec;

    if (to_sleep.tv_nsec < 0) {
        to_sleep.tv_sec--;
        to_sleep.tv_nsec += 1000000000;
    }

    if (to_sleep.tv_sec < 0) {
        to_sleep.tv_sec = 0;
        to_sleep.tv_nsec = 0;
    }

    return nanosleep(&to_sleep, NULL);
}
